package com.guodi.map.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.HttpRequester;
import com.guodi.common.utils.HttpRespons;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapLabel;
import com.guodi.map.service.IMapLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.*;


/**
 * @描述：地图标注控制类
 * @作者: 代鹏刚
 * @日期: 2020-03-30
 */
@RestController
@RequestMapping("/MapLabel")
@Api(value = "API - MapLabelController", tags = "地图标注")
public class MapLabelController extends BaseController {

    @Autowired
    public IMapLabelService mapLabelService;

    /**
    * @描述: 根据实体属性分页查询
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020-03-30
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "标注名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "专题编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "creator", value = "创建人", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "标注类型", required = false, dataType = "string", paramType = "query")
    })
    public R listByEntity(Page page, @ApiIgnore MapLabel mapLabel){
        List<MapLabel> mapLabelList = mapLabelService.listByEntity(page,mapLabel);
        page.setRecords(mapLabelList);
        page.setTotal(mapLabelList.size());
        return R.data(page);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-03-30
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R findById(String id) {
        if(StringUtils.isBlank(id)){
            return R.fail(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        MapLabel mapLabel = mapLabelService.getById(id);

        HttpRequester httpRequester = new HttpRequester();
        HttpRespons respons = null;
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("queryParameters", "{\"queryParams\":[{\"attributeFilter\":\"SMID &gt; 0\", \"name\":\"县界@县界#2\"}], \"startRecord\":0, \"expectCount\":20, \"networkType\":\"LINE\", \"queryOption\":\"ATTRIBUTEANDGEOMETRY\"}");
            params.put("queryMode", "SqlQuery");
            params.put("bounds ", "{\"leftBottom\":{\"x\":0, \"y\":0}, \"rightTop\":{\"x\":100, \"y\":100}}");
            params.put("distance", "1");
            params.put("keywords", "");
            params.put("spatialQueryMode", "INTERSECT");
//            params.put("returnPostAction", "true");
//            params.put("getMethodForm", "true");
//            params.put("returnContent", "true");
            respons = httpRequester.sendPost2("http://192.168.20.82:8090/iserver/services/map-HN_XZQH/rest/maps/%E7%9C%81%E7%95%8C/queryResults.json?returnPostAction=true&getMethodForm=true&returnContent=true", params);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String json = respons.getContent();
        return R.data(mapLabel);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-03-30
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "标注名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "标注内容", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "专题编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "geometryText", value = "图形字符串", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "标注类型", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注",  dataType = "string", paramType = "query")
    })
    public R save(@ApiIgnore MapLabel mapLabel){
        prepareData(mapLabel, true);
        if(StringUtils.isBlank(mapLabel.getId())){
            mapLabel.setId(UuidUtil.get32UUID());
        }
        mapLabelService.save(mapLabel);
        return R.data(mapLabel);
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-03-30
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(MapLabel mapLabel) {
        prepareData(mapLabel, false);
        mapLabelService.updateById(mapLabel);
        return R.data(mapLabel);
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-03-30
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个使用逗号隔开", required = true, dataType = "string", paramType = "query")
    })
    public R delete(String ids){
        String[] id = ids.split(",");
        List<String> list = Arrays.asList(id);
        mapLabelService.removeByIds(list);
        return R.data(null);
    }


    private void prepareData(MapLabel mapLabel, boolean isSave) {
        if(isSave){
            mapLabel.setCreator(getUser().getUserId().toString());
            mapLabel.setCreateTime(new Date());
            //防止误修改
            mapLabel.setModifier(null);
            mapLabel.setModifyTime(null);
        }else{
            mapLabel.setModifier(getUser().getUserId().toString());
            mapLabel.setModifyTime(new Date());
            //防止误修改
            mapLabel.setCreator(null);
            mapLabel.setCreateTime(null);
        }
    }
}
